//
//  WATFLaunchView.swift
//  WiFiFaster
//
//  Created by one on 2023/12/28.
//

import UIKit

class WATFLaunchView: UIView {
    override init(frame: CGRect) {
        super.init(frame: UIScreen.main.bounds)
        
        addSubview(watf_bgView)
       
        addSubview(watf_bgprogressView)
        watf_bgprogressView.addSubview(watf_progressView)
        
        watf_bgView.snp.makeConstraints { make in
            make.bottom.left.width.equalToSuperview()
            make.top.equalToSuperview()
        }
        watf_bgprogressView.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.size.equalTo(CGSize(width: 274, height: 4))
            make.bottom.equalToSuperview().offset(-WATFCommon.watf_XBottomSpace-15)
        }
        NotificationCenter.default.addObserver(
            self,
            selector: #selector(self.watf_enterBackground),
            name: UIApplication.didEnterBackgroundNotification, object: nil)
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: - watf action
    @objc func watf_enterBackground() {
        if !watf_isBack && !WATFManager.watf_manager.watf_LaunchH{ // 首次进入后台，代表冷启动结束
           
        }
        watf_isBack = true
        
        WATFTimerCom.watf_cancelTask(watf_launchTimeName)
    }
    class func watf_showView() -> WATFLaunchView? {
        let window = WATFAppDelegate.watf_shared().watf_window
        var watf_launchView = window?.viewWithTag(300) as? WATFLaunchView
        if watf_launchView == nil {
            watf_launchView = WATFLaunchView(frame: UIScreen.main.bounds)
            watf_launchView?.tag = 300
            window?.addSubview(watf_launchView!)
        }
        watf_launchView?.alpha = 0
        watf_launchView?.watf_value = 0
        window?.bringSubviewToFront(watf_launchView!)
        UIView.animate(withDuration: 0.25) {
            watf_launchView?.alpha = 1
        } completion: { _ in
            watf_launchView?.watf_startLoading()
        }
        return watf_launchView
    }
    func watf_hiddenView() {
        watf_hiddenBlcok?()
        UIView.animate(withDuration: 0.25) {
            self.alpha = 0
        } completion: { _ in
            
        }
    }
    
    func watf_startLoading() {
        WATFManager.watf_manager.watf_ipFTVAction()
        watf_value = 0
        watf_speed = 10
        isHidden = false
        alpha = 1
        watf_launchTimeName = WATFTimerCom.watf_timeTask(self, selector: #selector(watf_timeSum), start: 0, interval: 0.01, repeats: true, async: false, name: watf_launchTimeName)
    }
    
    func watf_stopLoading() {
        WATFTimerCom.watf_cancelTask(watf_launchTimeName)
        watf_value = 1
        watf_hiddenView()
    }
    
    func watf_cancelLoading() {
        WATFTimerCom.watf_cancelTask(watf_launchTimeName)
        watf_value = 0
        isHidden = true
    }
    
    @objc func watf_timeSum()  {
        if watf_value >= 274 {
            watf_stopLoading()
            return
        }
        watf_value += watf_step * watf_speed
    }
    
    
    //MARK: - watf po
    var watf_value: CGFloat = 0 {
        didSet {
            watf_progressView.frame = CGRect(x: 0, y: 0, width: watf_value, height: 4)
        }
    }
    var watf_step:CGFloat = 274 / CGFloat(15) * 0.01
    var watf_speed: CGFloat = 1
    var watf_launchTimeName: String = "watf_launchTimeName"
    var watf_hiddenBlcok:(()->())?
    var watf_isBack = false
    //MARK: - watf lazy
    lazy var watf_bgView: UIImageView = {
        let watf_bgView = UIImageView(image: UIImage(named: "watf_launch_bg"))
        watf_bgView.contentMode = .scaleAspectFill
        return watf_bgView
    }()
    
    lazy var watf_bgprogressView: UIView = {
        let watf_bgprogressView = UIView()
        watf_bgprogressView.backgroundColor = WATFCommon.watf_getHXColorValue(rgb: 0xFFFFFF, a: 0.2)
        return watf_bgprogressView
    }()
    lazy var watf_progressView: UIView = {
        let watf_progressView = UIView()
        watf_progressView.backgroundColor = WATFCommon.watf_getHXColorValue(rgb: 0xFF7A00)
        return watf_progressView
    }()
}
